<?php

/**
 * products actions.
 *
 * @package    baumarktforsibers.kg
 * @subpackage homepage
 * @author     Belitskii Ivan
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class productsActions extends sfActions
{
    
    /**
     * Execution of menu page
     * 
     * @param sfWebRequest $request 
     */
    public function executeViewproducts(sfWebRequest $request) {
        $time = date("H:i:s", time());
        $min_time = date("H:i:s", mktime(sfConfig::get('app_mintime'), "0", "0"));
        $max_time = date("H:i:s", mktime(sfConfig::get('app_maxtime'), "0", "0"));
        if (($time > $min_time && $time < $max_time) || $this->getUser()->isAuthenticated() == false) {
            $user = $this->getUser();
            $this->breakfasts = Doctrine_Core::getTable('products')->getByCategoryIdAndOrder(1);
            $this->first_dishes = Doctrine_Core::getTable('products')->getByCategoryIdAndOrder(2);
            $this->salads = Doctrine_Core::getTable('products')->getByCategoryIdAndOrder(3);
            $this->second_dishes = Doctrine_Core::getTable('products')->getByCategoryIdAndOrder(4);
            $this->garnirs = Doctrine_Core::getTable('products')->getByCategoryIdAndOrder(5);
            $this->lotoks = Doctrine_Core::getTable('products')->getByCategoryIdAndOrder(6);
            $this->buhlos  = Doctrine_Core::getTable('products')->getByCategoryIdAndOrder(7);
            $this->zapivons  = Doctrine_Core::getTable('products')->getByCategoryIdAndOrder(8);
            $this->user = $this->getUser();
            $orders = $this->getUser()->getAttribute('orders');
            $this->user = $user;
            $orders = $user->getAttribute('orders');
            $this->orders = $orders;
            $orders_combined = null;
            if ($user->hasAttribute('orders_combined')) {
                $orders_combined = $user->getAttribute('orders_combined');
            }
            $this->orders_combined = $orders_combined;
        }
        else {
            $this->redirect("@restrict");
        }
    }
    
    
    
    /**
     *
     * Execution of view order page
     * 
     * @param sfWebRequest $request 
     */
    public function executeVieworder(sfWebRequest $request) {
        if (!$this->getUser()->isAuthenticated()) {
            $this->redirect("@viewproducts");
        }
        $time = date("H:i:s", time());
        $min_time = date("H:i:s", mktime(sfConfig::get('app_mintime'), "0", "0"));
        $max_time = date("H:i:s", mktime(sfConfig::get('app_maxtime'), "0", "0"));
        if (($time > $min_time && $time < $max_time) || $this->getUser()->isAuthenticated() == false) {
            
            // Define if user decided to sort by Category.
            // If attribute in sessions is not empty, then allow to sort By Category
            $user = $this->getUser();
            $orders = Doctrine::getTable('orders')->getTodaysOrder("ASC")->toArray();
            $category_sort = 0;
            $allowSortByCategory = $user->getAttribute('allowToSortByCategory') != '' ? true : false;
            if ($request->hasParameter('sortbyname')) {

                $sorts = $request->getParameter('sortbyname') == '1' ? 'ASC' : 'DESC';            
                $user->setAttribute('sortbyname', $sorts);

                // select referrals and sort them by username
                $orders = Doctrine::getTable('orders')->getTodaysOrder($sorts)->toArray();

                // if user has chosen to sort by username, then prohibit to sort by joined date
                $user->setAttribute('allowToSortByCategory', false);
                $this->redirect('@view_order');
            } 
            elseif ($request->hasParameter('sortbycategory')) {
                $categorysorts = $request->getParameter('sortbycategory') == '1' ? 'ASC' : 'DESC';
                $user->setAttribute('sortbycategory', $categorysorts);

                // select referrals and sort them by joined date
               // $referrals = Doctrine_Core::getTable('sfGuardUser')->getRefferalsSortByJoinDate($userId, $categorysorts);
                $orders = Doctrine::getTable('orders')->getTodaysOrderSortByCategory($categorysorts)->toArray();

                // if user has chosen to sort by joined date, then allow to sort by joined date
                $user->setAttribute('allowToSortByCategory', true);
                $this->redirect('@view_order');
            }
            
            // set referrals depending on the username sort.
            // if sort is ASC, then order referrals by ASC
            // else order by DESC
            if ($this->getUser()->getAttribute('sortbyname') != '') {
                $sort = $user->getAttribute('sortbyname');
                $orders = Doctrine::getTable('orders')->getTodaysOrder($sort)->toArray();
                //$referrals = Doctrine_Core::getTable('sfGuardUser')->getRefferals($userId, $sort);
            } else {
                $sort = 'DESC';
                $user->setAttribute('sortbyname', $sort);
                $orders = Doctrine::getTable('orders')->getTodaysOrder($sort)->toArray();
                //$referrals = Doctrine_Core::getTable('sfGuardUser')->getRefferals($userId, $sort);
            }           

            // check if it is allowed to sort by joined date
            // set referrals depending on the joined date sort.
            // if sort is ASC, then order referrals by ASC
            // else order by DESC
            if ($user->getAttribute('sortbycategory') != '' && $allowSortByCategory) {
                $category_sort = $user->getAttribute('sortbycategory');
                $orders = Doctrine::getTable('orders')->getTodaysOrderSortByCategory($category_sort)->toArray();
                //$referrals = Doctrine_Core::getTable('sfGuardUser')->getRefferalsSortByJoinDate($userId, $category_sort);
            } elseif (isset($allowToSortByDate)) {
                $category_sort = 'DESC';
                $guser->setAttribute('sortbycategory', $category_sort);
                $orders = Doctrine::getTable('orders')->getTodaysOrderSortByCategory($category_sort)->toArray();
                //$referrals = Doctrine_Core::getTable('sfGuardUser')->getRefferalsSortByJoinDate($userId, $category_sort);
            }

            // set username and date sort for the view
            $this->username_sort = $sort == 'DESC' ? 1 : 0;
            $this->category_sort = $category_sort == 'DESC' ? 1 : 0;
            
            // get ids of the users who ordered today
            $this->user_order_ids = Doctrine::getTable('orders')->getUsersWhoOrderedToday();
            $this->user_combined_order_ids = Doctrine::getTable('orders_combined')->getUsersWhoOrderedToday();
            
            $user_order_ids = array();
            foreach($this->user_order_ids as $uuids) {
                $user_order_ids[] = $uuids->getUser()->getId();
            }
            $user_combined_order_ids = array();
            foreach($this->user_combined_order_ids as $uuids) {
                $user_order_ids[] = $uuids->getUser()->getId();
            }
            
            // remove repeating ids
            $user_order_ids = array_unique($user_order_ids);
            $this->users_order_ids = $user_order_ids;
            
            $this->currentId = $this->getUser()->getGuardUser()->getId();
            
            $this->orders = $orders;//Doctrine::getTable('orders')->getTodaysOrder()->toArray();
            $this->orders_combined = Doctrine::getTable('orders_combined')->getCombinedOrders();
            
        }
        else {
            $this->redirect("@restrict");
        }
    }
    
    
    
    /**
     *
     * Execution of check order page
     * 
     * @param sfWebRequest $request 
     */
    public function executePreorder(sfWebRequest $request) {
        if (!$this->getUser()->isAuthenticated()) {
            $this->redirect("@viewproducts");
        }
        $time = date("H:i:s", time());
        $min_time = date("H:i:s", mktime(sfConfig::get('app_mintime'), "0", "0"));
        $max_time = date("H:i:s", mktime(sfConfig::get('app_maxtime'), "0", "0"));
        if (($time > $min_time && $time < $max_time) || $this->getUser()->isAuthenticated() == false) {
            $this->orders = Doctrine::getTable('orders')->getTodaysOrder()->toArray();
            $this->order_of_each_user = Doctrine::getTable('orders')->getOrderOfEachUser()->toArray();
        }
        else {
            $this->redirect("@restrict");
        }
    }
    
    public function executeSendSMS(){
        $orders = Doctrine::getTable('orders')->getTodaysOrderSortByCategory($categorysorts)->toArray();
        $str    = "";
        foreach ($orders as $order):
            $str .= $order['Product']['name']."-".$order['quantity'].",";
        endforeach;
        
        $str = substr($str, 0, -1);

        $message = Swift_Message::newInstance()
                    ->setFrom('edaorder'.rand(1, 45)."@eda.kg")
                    ->setTo("996551143828@sms.megacom.kg")
                    ->setSubject('zakaz')
                    ->setBody($str);

        $this->getMailer()->send($message);
        
        return sfView::NONE;
    }
}
